/****************************************************
	文件：RedSpotSys.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/25 13:43:34
	功能：红点系统
*****************************************************/

using System;
using System.Collections.Generic;
using HTUtility;
using UnityEngine;

namespace RedSpotSystem
{
    public class RedSpotSys : Singleton<RedSpotSys>
    {
        /// <summary>
        /// 共外部注册的红点数量变化时的事件
        /// </summary>
        /// <param name="self">传入红点数据</param>
        public delegate void RedSpotEvent(RedSpotData self);

        /// <summary>
        /// 缓存所有红点数据
        /// </summary>
        private Dictionary<GEnum.E_UIComp, RedSpotData> mRedDataDict;

        /// <summary>
        /// 获取红点数据
        /// </summary>
        /// <param name="eComp"></param>
        /// <returns></returns>
        public RedSpotData this[GEnum.E_UIComp eComp]
        {
            get
            {
                RedSpotData data = null;
                if (mRedDataDict.TryGetValue(eComp, out data) == false || data == null)
                {
                    HTLogger.Error("RedSpotSys 的缓存中不存在 RedSpotData：{0}", eComp.ToString());
                    return null;
                }
                return data;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            mRedDataDict = new Dictionary<GEnum.E_UIComp, RedSpotData>();
            LoadRedSpotData();
            HTLogger.Info("RedSpotSys init done.");
        }
        /// <summary>
        /// 获取红点数据
        /// </summary>
        /// <param name="eComp">UIComp枚举</param>
        /// <returns></returns>
        public RedSpotData GetRedData(GEnum.E_UIComp eComp)
        {
            RedSpotData data = null;
            if (mRedDataDict.TryGetValue(eComp, out data) == false || data == null)
            {
                HTLogger.Error("RedSpotSys 的缓存中不存在 RedSpotData：{0}", eComp.ToString());
                return null;
            }
            return data;
        }

        /// <summary>
        /// 加载红点数据
        /// </summary>
        private void LoadRedSpotData()
        {
            RedSpotDataScheme scheme = (RedSpotDataScheme)SchemesMgr.Instance.Get(GEnum.E_Scheme.RedSpotData);
            foreach (RedSpotDataItem item in scheme.RedSpotDataList)
            {
                RedSpotData data = new RedSpotData();
                data.GModule = item.BelongModule;
                data.Parent = item.Parent;
                data.Self = item.Self;
                data.IsLeaf = item.IsLeaf == 1;
                if (mRedDataDict.ContainsKey(data.Self))
                {
                    HTLogger.Error("重复加载红点数据：{0}", Enum.GetName(typeof(GEnum.E_UIComp), data.Self));
                }
                else
                {
                    mRedDataDict.Add(data.Self, data);
                }
            }
        }
    }
}
